/*!
 * New Launch.
 * Date: 2015/12/29.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg) {
  $(function () {
    var $dataFrm = $('#data-frm'),
      $submitBtn = $dataFrm.find('button[type="submit"]'),
      dataFieldRule, options, dataValidator;

    dataFieldRule = {
      creative_name:{
        rule:{
            required:{
                message:VAM.j('Creative cannot be empty')
            }
        },
        messageContainer:'creative_name_err'
      },
      audit_domain:{
        rule:{
          required:{
            message:VAM.j('Audit domain cannot be empty')
          }
        },
        messageContainer:'audit_domain_err'
      },
      custom_creative_width:{
        rule:{
          required:{
            message: VAM.j('Width cannot be empty')
          },
          intnumber:{
            message: VAM.j('Input correct number format')
          },
          minval:{
            param: parseFloat('1'),
            message: VAM.j('Width not less than 1px')
          },
          maxval:{
            param: parseFloat('2000'),
            message:VAM.j('Width not more than 2000px')
          }
        },
        messageContainer:'custom_creative_width_err'
      },
      custom_creative_height:{
        rule:{
          required:{
            message:VAM.j('Height cannot be empty')
          },
          intnumber:{
            message: VAM.j('Input the correct number format')
          },
          minval:{
            param: parseFloat('1'),
            message: VAM.j('The value can not less than 1px')
          },
          maxval:{
            param: parseFloat('2000'),
            message: VAM.j('Height not more than 2000px')
          }
        },
        messageContainer:'custom_creative_height_err'
      },
      custom_creative_code:{
        rule:{
            required:{
                message:VAM.j('Custom creative code cannot be empty')
            }
        },
        messageContainer:'custom_creative_code_err'
      },
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
          $dataFrm.ajaxSubmit({
            beforeSubmit: function() {
              $submitBtn.button('loading');
            },
            success: function (data) {
              $submitBtn.button('reset');
              if (data.status === 'success') {
                VamOpMsg.messager(data);
                window.location.href = 'creative-list';
              } else {
                VamOpMsg.messager(data);
              }
            }
          });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
    dataValidator = formValidator.dataValidator;
    dataValidator.addRule('effectiveNumber',function(val, opt){
      return !!(val && /^\d+(\.\d{1,2})?$/.test(val));
    });
  });
  //Creative Vocation.
  $(function(){
    var creativeVocation = $.parseJSON(VAM.getVar('CREATIVE_VOCATION')),
      $vocationLarge = $('#creative_vocation_large'),
      $vocationSmall = $('#creative_vocation_small');
    for(var key in creativeVocation){
      if(creativeVocation.hasOwnProperty(key)) {
        $vocationLarge.append('<option value="'+key+'">' + key + '</option>');
      }
    }

    $vocationLarge.on('change',function(){
      var $me = $(this),
        vocationSmall = creativeVocation[$me.val()],
        i,j;
      $vocationSmall.children().remove();
      $vocationSmall.append('<option value="' + VAM.j('All') + '">' + VAM.j('All') + '</option>');
      for (i=0, j=vocationSmall.length; i<j; i++) {
        $vocationSmall.append('<option value="' + vocationSmall[i] + '">' + vocationSmall[i] +'</option>');
      }
    });

    var creativeParentVocation = VAM.getVar('parent_category');
    var creativeChildVocation = VAM.getVar('child_category');

    $("#creative_vocation_large option[value='"+creativeParentVocation+"']").attr('selected', "selected");
    $vocationLarge.trigger('change');
    $("#creative_vocation_small option[value='"+creativeChildVocation+"']").attr('selected', "selected");
  })
});
